package edu.tum.cs.i1.pse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class CacheProxy implements ConnectorInterface {

	private ConnectorInterface connector;
	private ConnectorInterface cache;
	
	@Override
	public InputStream open(URL url) {
		if (cache == null)
			cache = new Cache();
		
		if (Cache.hasRessource(url))
			return cache.open(url);
		else
		{
			if (connector == null)
				connector = new RealConnector();
			InputStream is = connector.open(url);
			Cache.cacheRessource(is, url);
			return is;
		}
	}

	@Override
	public void close(InputStream stream) throws IOException {
		stream.close();
	}

}
